import { Sequelize } from "sequelize";
import { initUserModel, User } from "./User";
import { initUserSessionModel, UserSession, defineUserSessionAssociations } from "./UserSession";
import { initLoginLogModel, LoginLog, defineLoginLogAssociations } from "./LoginLog";
import { initDocumentModel, Document, defineDocumentAssociations } from "./Document";
import {
  initDocumentCollaborationModel,
  DocumentCollaboration,
  defineDocumentCollaborationAssociations
} from "./DocumentCollaboration";
import { initYjsDocumentModel, YjsDocument, defineYjsDocumentAssociations } from "./YjsDocument";

/**
 * 数据库模型索引文件
 * 统一管理所有模型的初始化和关联关系
 */

/**
 * 初始化所有数据库模型
 */
export function initModels(sequelize: Sequelize): {
  User: typeof User;
  UserSession: typeof UserSession;
  LoginLog: typeof LoginLog;
  Document: typeof Document;
  DocumentCollaboration: typeof DocumentCollaboration;
  YjsDocument: typeof YjsDocument;
} {
  // 初始化所有模型
  const UserModel = initUserModel(sequelize);
  const UserSessionModel = initUserSessionModel(sequelize);
  const LoginLogModel = initLoginLogModel(sequelize);
  const DocumentModel = initDocumentModel(sequelize);
  const DocumentCollaborationModel = initDocumentCollaborationModel(sequelize);
  const YjsDocumentModel = initYjsDocumentModel(sequelize);

  // 定义关联关系
  defineUserSessionAssociations();
  defineLoginLogAssociations();
  defineDocumentAssociations();
  defineDocumentCollaborationAssociations();
  defineYjsDocumentAssociations();

  return {
    User: UserModel,
    UserSession: UserSessionModel,
    LoginLog: LoginLogModel,
    Document: DocumentModel,
    DocumentCollaboration: DocumentCollaborationModel,
    YjsDocument: YjsDocumentModel
  };
}

// 导出模型类
export { User, UserSession, LoginLog, Document, DocumentCollaboration, YjsDocument };
export type { UserAttributes } from "./User";
export type { UserSessionAttributes } from "./UserSession";
export type { LoginLogAttributes } from "./LoginLog";
export type { DocumentAttributes } from "./Document";
export type { DocumentCollaborationAttributes } from "./DocumentCollaboration";
export type { YjsDocumentAttributes } from "./YjsDocument";
